﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevExpress.XtraEditors;

namespace QuanLyThuNgan
{
    public partial class frm_BenhNhanNoiTru : DevExpress.XtraEditors.XtraForm
    {
        public frm_BenhNhanNoiTru()
        {
            InitializeComponent();
        }
        DonVi_Khac dv_khac = new DonVi_Khac();
        ThuDTLDTO thudtl = new ThuDTLDTO();
        ThuDTLCTL thudata = new ThuDTLCTL();
        DataTable dtLoadDSPhieu = new DataTable();
        BenhNhanInfo bn = new BenhNhanInfo();
        DataTable dtkt = new DataTable();
        int row_dsbn = 0;
        private void frm_BenhNhanNoiTru_Load(object sender, EventArgs e)
        {
            LoadDSTamUng();
            this.Focus();
            txtSoHoSo_DS.Focus();
        }
        private void LoadDSTamUng()
        {
            CaThuNganCTL ctl = new CaThuNganCTL();
            dtkt = ctl.m_SelectKiemTra(frmLogin.nd.IDNguoiDung);
            DataTable dtDonVi = dv_khac.DMDonVi();
            ClassHelp.m_SearchLookUpEdit(dtDonVi, searchLookUpEditKhoa, "TenDonVi", "MaDonVi");
            
        }
        private void btnTimKiem_DS_Click(object sender, EventArgs e)
        {
            if (txtHoTen_DS.Text == "" && txtSoHoSo_DS.Text == "")
                if (txtSoNhapVien_DS.Text == "" && searchLookUpEditKhoa.Text == "")
                    if(txtNamSinh_DS.Text =="" && txtTen.Text=="")
                       return;    
            //if(searchLookUpEditKhoa.EditValue.ToString()==         
            DataTable dtdsbenhnhan = dv_khac.TimKiemBNNoiTru_TamUng(txtSoHoSo_DS.Text,txtHoTen_DS.Text,txtTen.Text,
            txtNamSinh_DS.Text == "" ? 0: Uit.it_Parse.ToInteger(txtNamSinh_DS.Text),txtSoNhapVien_DS.Text,
            searchLookUpEditKhoa.Text == "" ? "" : searchLookUpEditKhoa.EditValue.ToString());
            gridControl_DSBN.DataSource = dtdsbenhnhan;
        }

        private void btnCancel_Click(object sender, EventArgs e)
        {
           txtHoTen_DS.Text = "" ;
           txtSoHoSo_DS.Text = "";
           txtSoNhapVien_DS.Text = "";
           searchLookUpEditKhoa.EditValue = -1;
           txtNamSinh_DS.Text ="";
           txtTen.Text = "";
        }
        private void OpenTap(string NameTap)
        {
            for (int i = 0; i < xtraTabControlNoiTru.TabPages.Count; i++)
            {
                if (xtraTabControlNoiTru.TabPages[i].Name.Equals(NameTap))
                {
                    xtraTabControlNoiTru.TabPages[i].PageEnabled = true;
                    // xtraTabControlChiPhiNGTru.TabPages[i].Focus();
                }
                else
                    xtraTabControlNoiTru.TabPages[i].PageEnabled = false;
            }
        }

        private void xtraTabControlNoiTru_SelectedPageChanged(object sender, DevExpress.XtraTab.TabPageChangedEventArgs e)
        {
            if (xtraTabControlNoiTru.SelectedTabPage == xtraTabPage_DSBN)
            {
                txtSoHoSo_DS.Focus();
            }
            if (xtraTabControlNoiTru.SelectedTabPage == xtraTabPage_Nhap)
            {
              
                dtLoadDSPhieu = dv_khac.DSPhieuNopTien(thudtl.Ssohs, thudtl.Ssonhapvien);
                gridControlNhap.DataSource = dtLoadDSPhieu;
                txtSoHoSo.Text = thudtl.Ssohs;
                txtHoTen.Text = bn.HoTen;
                txtGioiTinh.Text = bn.Gioi;
                txtNamSinh.Text = bn.NamSinh.ToString();
                txtChuanDoanNhapVien.Text = bn.DiaChi2;
                txtSoBienNhan.Text = "";
                txtNgayNop.Text = Uit.it_Date.CurrentDate("dd/MM/yyyy");
                txtLanNop.Text = "";
                txtSoTienNop.Text = "";
                txtSoNhanVien.Text = thudtl.Ssonhapvien;
                txtNgayNhapVien.Text = bn.Ngaynhapvien;
                txtNgayXuatVien.Text = bn.Ngayxuatvien;
                txtTraiBenh.Text = bn.Traibenh;
                DataRow dro = gridViewBNNTru.GetDataRow(0);
                if (dro == null) return;
                txtSoTienNop.Text = dro["sotien"].ToString();
                txtSoBienNhan.Text = dro["sobn"].ToString();
            }
            
        }

        private void gridView_DSBN_DoubleClick(object sender, EventArgs e)
        {
            DataRow dr = gridView_DSBN.GetDataRow(gridView_DSBN.FocusedRowHandle);
            if (dr == null) return;
            thudtl.Ssohs = dr["SoHS"].ToString();
            thudtl.Ssonhapvien = dr["sonhapvien"].ToString();
            thudtl.Ssobn = "";
            bn.HoTen = dr["Ho"].ToString() + " " + dr["Ten"].ToString();
            bn.Gioi = dr["GioiTinh"].ToString();
            bn.NamSinh = Uit.it_Parse.ToInteger(dr["NamSinh"]);
            bn.DiaChi2 = dr["ChanDoan"].ToString();//DiaChi Thay Tam Cho Dia Chi
            bn.Ngaynhapvien = dr["ngaynhapvien"].ToString();
            bn.Ngayxuatvien = dr["ngayxuatvien"].ToString();
            bn.DiaChi = dr["DiaChi"].ToString();
            searchLookUpEditKhoa.EditValue = dr["traibenh"];
            bn.Traibenh = searchLookUpEditKhoa.Text;
            OpenTap(xtraTabPage_Nhap.Name);
        }
        private void gridViewBNNTru_DoubleClick(object sender, EventArgs e)
        {
            DataRow dr = gridView_DSBN.GetDataRow(gridView_DSBN.FocusedRowHandle);
            if (dr == null) return;
            txtSoBienNhan.Text = dr["Sobn"].ToString();
            txtNgayNop.Text = dr["ngay"].ToString();
            txtLanNop.Text = dr["Stt"].ToString();
            txtSoTienNop.Text = dr["sotien"].ToString();
        }
        private void btnSave_Click(object sender, EventArgs e)
        {
            if (txtSoTienNop.Text == "") return ;
            thudtl.Fsotien = float.Parse(txtSoTienNop.Text);
            thudtl.Sngaynop = Uit.it_Date.CurrentDate("yyyy-MM-dd");
            if (thudtl.Fsotien > 0.0)
            {
                    thudtl.Snoivao = dtkt.Rows[0]["MaDonVi"].ToString();
                    if (Uit.it_Parse.ToInteger(dtkt.Rows[0]["loai"]) > 0)
                    {
                        if (txtSoBienNhan.Text == "" && txtSoTienNop.Text != "")
                        {
                            object sobiennhan = thudata.m_Insert(thudtl, frmLogin.nd.IDNguoiDung, Uit.it_Parse.ToInteger(dtkt.Rows[0]["IDCaThuNgan"]));
                            txtSoBienNhan.Text = sobiennhan.ToString();
                        }
                        if (txtSoBienNhan.Text != "") // update 
                            if (thudata.m_Update(txtSoBienNhan.Text, thudtl.Fsotien))
                                XtraMessageBox.Show("Update Thành Công");
                        dtLoadDSPhieu = dv_khac.DSPhieuNopTien(thudtl.Ssohs, thudtl.Ssonhapvien);
                        gridControlNhap.DataSource = dtLoadDSPhieu;
                    }
                    else
                        XtraMessageBox.Show("Bạn Không có quyền");
                     
            }
        }
        private void gridViewBNNTru_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            DataRow dr = gridViewBNNTru.GetDataRow(e.FocusedRowHandle);
            if(dr==null) return;
            txtSoTienNop.Text = dr["sotien"].ToString();
            txtSoBienNhan.Text = dr["sobn"].ToString();
            txtLanNop.Text = dr["Stt"].ToString();
            thudtl.Tenmucphi = dr["TenMucPhi"].ToString();
            thudtl.Ifeeid = int.Parse(dr["feeid"].ToString());
            if (thudtl.Ifeeid == 5)
                txtSoTienNop.Enabled = false;
            else
                txtSoTienNop.Enabled = true;
        }

        private void gridView_DSBN_KeyUp(object sender, KeyEventArgs e)
        {
            if (e.KeyData == Keys.Enter)
            {
                DataRow dr = gridView_DSBN.GetDataRow(row_dsbn);
                thudtl.Ssohs = dr["SoHS"].ToString();
                thudtl.Ssonhapvien = dr["sonhapvien"].ToString();
                thudtl.Ssobn = "";
                bn.HoTen = dr["Ho"].ToString() + " " + dr["Ten"].ToString();
                bn.Gioi = dr["GioiTinh"].ToString();
                bn.NamSinh = Uit.it_Parse.ToInteger(dr["NamSinh"]);
                bn.DiaChi2 = dr["ChanDoan"].ToString();//DiaChi Thay Tam Cho Dia Chi
                bn.Ngaynhapvien = dr["ngaynhapvien"].ToString();
                bn.Ngayxuatvien = dr["ngayxuatvien"].ToString();
                bn.DiaChi = dr["DiaChi"].ToString();
                searchLookUpEditKhoa.EditValue = dr["traibenh"];
                bn.Traibenh = searchLookUpEditKhoa.Text;
                OpenTap(xtraTabPage_Nhap.Name);
            }
        }
        private void gridView_DSBN_FocusedRowChanged(object sender, DevExpress.XtraGrid.Views.Base.FocusedRowChangedEventArgs e)
        {
            row_dsbn = e.FocusedRowHandle;
        }

        private void btnDSBNKhac_Click(object sender, EventArgs e)
        {
            OpenTap(xtraTabPage_DSBN.Name);
        }

        private void btnLuuIn_Click(object sender, EventArgs e)
        {
            rpt_ThuTienNoiTru rpt = new rpt_ThuTienNoiTru();
            rpt.donvithu = thudtl.Snoivao;
            rpt.sohoso = txtSoHoSo.Text;
            rpt.sobn = txtSoBienNhan.Text;
            rpt.ngayin = txtNgayNop.Text;
            rpt.hotenbn = txtHoTen.Text;
            int tuoi = int.Parse(Uit.it_Date.CurrentYear().ToString())- int.Parse(txtNamSinh.Text);
            rpt.tuoi = tuoi.ToString();
            rpt.diachi = bn.DiaChi;
            rpt.donvikhambenh = txtTraiBenh.Text;
            rpt.sonhapvien = txtSoNhanVien.Text;
            rpt.sotienthu = txtSoTienNop.Text;
            rpt.khoanthu = thudtl.Tenmucphi;
            Clsdocso docs = new Clsdocso();
            rpt.sotienbbangchu = docs.Doc(long.Parse(txtSoTienNop.Text));
            rpt.tennguoilapphieu = frmLogin.nd.TenNguoiDung;
            //XtraMessageBox.Show(frmLogin.nd.TenNguoiDung);
            rpt.ngaythang = "Ngày " + txtNgayNop.Text;
            rpt.gioingay = Uit.it_Date.CurrentDate("hh:mm") + " Ngày " + txtNgayNop.Text;
            rpt.tenthungan = dtkt.Rows[0]["TenNguoiXacNhan"].ToString();
            rpt.ShowPreview();    
        }
    }
}